package com.kungfuhacking.wristbandpro.my.presenter;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.location.bean.OldEntity;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.my.view.IFamilyView;
import com.kungfuhacking.wristbandpro.net.BaseListObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;

import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by Aaron on 17/5/15.
 */

public class IFamilyPresenter implements BasePresenter {

    private IFamilyView iFamilyView;

    public IFamilyPresenter(IFamilyView iFamilyView) {
        this.iFamilyView = iFamilyView;
    }

    public void familyInfo() {

        JSONObject j = new JSONObject();
        j.put("token", iFamilyView.getToken());

        RetrofitHttpUtil.getInstance().getChildrenInfo(new BaseListObserver<OldEntity>() {
            @Override
            public void onSubscribe(Disposable d) {
                iFamilyView.showLoading("获取中...");
                compositeDisposable.add(d);
            }

            @Override
            public void onHandleSuccess(List<OldEntity> oldEntities) {
                iFamilyView.hideLoading();
                iFamilyView.setAdapterData(oldEntities);
            }

            @Override
            public void onHandleError(String message) {
                iFamilyView.hideLoading();
                iFamilyView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iFamilyView.hideLoading();
                iFamilyView.showErr(message);
                iFamilyView.nextActivityWithSingleTask(LoginActivity.class);
            }
        }, new RequestBean().setData(j.toString()));

    }

    @Override
    public void clearRequest() {
        iFamilyView.hideLoading();
        compositeDisposable.clear();
    }
}
